﻿// 7-18 求二叉树的叶子结点个数 (20 分).cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//user:lidongcheng
//key:看内容中的注释，关键是在判断叶子节点那里
//date:20191113
#include <iostream>
#include<string>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
class TreeNode
{

public:
	char data;
	TreeNode* lchild;
	TreeNode* rchild;

	TreeNode()

	{
		data = 0;
		TreeNode* lchild = NULL;
		TreeNode* rchild = NULL;
	}

	void write(string s, int& i)
	{
		//cin >> data;
		data = s[i];
		i++;
		if (data != '#')
		{
			this->lchild = new TreeNode;
			this->rchild = new TreeNode;
		}
	}

	void visit_read(int&j) 
	{

		if (data != '#')//这里是最关键的啊
		{//必须是是个有数据的节点才能去检测他的两个叶子的数据是不是#
			cout << this->data;
			if ((this->lchild->data == '#' && this->rchild->data == '#'))
			{
				j++;
			}
		}
	

	}

	void pretraversal(TreeNode* proot, string s, int& i) {
		if (NULL != proot)
		{
			(proot)->write(s, i);
			pretraversal(proot->lchild, s, i);
			pretraversal(proot->rchild, s, i);
		}
	}

	void pretraversal_middle_read(TreeNode* proot, int& j) {
		if (NULL != proot)
		{
			pretraversal_middle_read(proot->lchild, j);
			(proot)->visit_read(j);
			pretraversal_middle_read(proot->rchild, j);
		}

			
		
	}
};






int main() {

	int i = 0;
	int j = 0;

	string s;

	getline(cin, s);

	i = 0;


	TreeNode* head = new TreeNode;

	head->pretraversal(head, s, i);//建立二叉树

	head->pretraversal_middle_read(head, j);//遍历二叉树
	cout << "\n";
	cout << j;


}